/** @type {import('@sveltejs/kit').RequestHandler} */
export async function get (event) {
    const res = await fetch("https://jsonplaceholder.typicode.com/photos")
    const photos = await res.json()

    return {
        body: {
            photos: photos.slice(0, 100)
        }
    }

}

/** @type {import('./index').RequestHandler} */
export async function post ({ request }) {
    const data = await request.formData()
    const title = /** @type {string} */ (data.get("title"))
    const file = /** @type {File} */ (data.get("file"))

    /** @type {Record<string,string>} */
    const errors = {}
    const values = { title }

    if (title.length < 10) {
        errors.title = "Title is too short"
    }

    if (file.size > 100000) {
        errors.file = "File is too large"
    }
    if (Object.keys(errors).length) {
        return {
            status: 400,
            body: {
                errors, values
            }
        }

    }
    return {
        status: 303,
        Headers: {
            location: "/photos/99"
        }
    }
}